package com.ibeeking.found.cms.b.rest.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.cms.api.common.dto.*;
import com.ibeeking.found.cms.api.common.query.*;
import com.ibeeking.found.cms.api.feign.*;
import com.ibeeking.found.cms.b.rest.service.IPageBaseService;
import com.ibeeking.found.cms.b.rest.service.IPageModuleService;
import com.ibeeking.found.cms.service.common.bo.DataFieldConfigBO;
import com.ibeeking.found.cms.service.common.bo.PageBaseBO;
import com.ibeeking.found.cms.service.common.bo.PageModuleBO;
import com.ibeeking.found.cms.service.common.constants.Constant;
import com.ibeeking.found.cms.service.common.constants.enums.PageCategoryEnum;
import com.ibeeking.found.cms.service.common.constants.enums.PageModuleDataEnum;
import com.ibeeking.found.cms.service.common.constants.enums.PageModuleTypeEnum;
import com.ibeeking.found.cms.service.common.dos.PageModuleDO;
import com.ibeeking.found.cms.service.common.dos.PageModuleDraftDO;
import com.ibeeking.found.cms.service.common.param.*;
import com.ibeeking.found.cms.service.common.query.*;
import com.ibeeking.found.cms.service.common.vo.*;
import com.ibeeking.found.cms.service.mapper.PageModuleDraftMapper;
import com.ibeeking.found.cms.service.mapper.PageModuleMapper;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.common.enums.PageTypeEnum;
import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.enums.SystemEnum;
import com.ibeeking.found.common.enums.goods.GoodsChannelEnum;
import com.ibeeking.found.common.enums.goods.GoodsStatusEnum;
import com.ibeeking.found.common.enums.marketing.CampaignSceneEnum;
import com.ibeeking.found.common.enums.marketing.CampaignStatusEnum;
import com.ibeeking.found.common.enums.marketing.CheckStatusEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.found.common.utils.DateUtils;
import com.ibeeking.found.common.utils.NumberUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.ParamCheckUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static java.math.BigDecimal.ROUND_UNNECESSARY;

/**
 * @ClassName PageModuleServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-05-08 16:03
 **/
@Service
public class PageModuleServiceImpl extends ServiceImpl<PageModuleMapper, PageModuleDO> implements IPageModuleService {

    @Resource
    private PageModuleMapper pageModuleMapper;

    @Resource
    private PageModuleDraftMapper pageModuleDraftMapper;
    @Resource
    private IPageBaseService pageBaseServiceImpl;
    @Resource
    private CmsGoodsFeignBClient cmsGoodsFeignBClient;
    @Resource
    private CmsMarketingFeignBClient cmsMarketingFeignBClient;
    @Resource
    private CmsMarketingFeignCClient cmsMarketingFeignCClient;
    @Resource
    private CmsFeignCClient cmsFeignCClient;
    @Resource
    private CmsSystemCClient cmsSystemCClient;
    @Resource
    private CmsGoodsFeignCClient cmsGoodsFeignCClient;


    @Override
    public List<PageModuleVO> list(PageModuleQuery pageModuleQuery) {
        List<PageModuleDO> pageModuleDOs = pageModuleMapper.selectList(new LambdaQueryWrapper<PageModuleDO>()
                .eq(pageModuleQuery.getPageId() != null, PageModuleDO::getPageId, pageModuleQuery.getPageId()));
        return JsonUtils.jsonToList(pageModuleDOs, PageModuleVO.class);
    }

    @Override
    public Page<PageModuleVO> pageList(PageModulePageQuery pageModulePageQuery) {
        Page<PageModuleDO> pages = pageModuleMapper.selectPage(new Page<PageModuleDO>(), new LambdaQueryWrapper<PageModuleDO>());
        return BeanUtil.convertPage(pages, PageModuleVO.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageModuleAllView save(PageModuleAllParam param, CurrentUser currentUser) {
        DataUtils.trueThrowException(param == null, "参数为空!");
        Long id = param.getId();
        Long shopId = param.getShopId();
        Long userId = UserUtils.getUserId();
        String industry= currentUser.getIndustry();
        Integer operType = param.getOperType();
        Integer platform = param.getPlatform();
        if (LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())) {
            shopId = shopId == null ? UserUtils.getShopId() : shopId;
            platform = platform == null ? SystemEnum.SHOP.getValue() : platform;
        } else if (LoginRoleEnum.TENANT.name().equals(currentUser.getLoginSystem())) {
            shopId = shopId == null ? UserUtils.getShopId() : shopId;
            platform = platform == null ? SystemEnum.TENANT.getValue() : platform;
        } else {
            DataUtils.trueThrowException(true, "禁止进行当前操作!");
        }
        List<PageModuleParam> moduleList = param.getModuleList();
        DataUtils.trueThrowException(Collections3.isEmpty(moduleList), "请填写页面名称!");
        PageModuleParam pageModuleConfigParam = moduleList.get(0);
        DataUtils.trueThrowException(pageModuleConfigParam == null || !PageModuleTypeEnum.CONFIG.getValue().equalsIgnoreCase(pageModuleConfigParam.getType()), "请填写页面名称!");
        PageBaseParam pageBaseParam = new PageBaseParam();
        String dataField = pageModuleConfigParam.getDataField();
        DataFieldConfigBO dataFieldConfigBO = JsonUtils.fromJson(dataField, DataFieldConfigBO.class);
        DataUtils.trueThrowException(dataFieldConfigBO == null || StringUtils.isBlank(dataFieldConfigBO.getTitle()), "请填写页面名称!");
        Integer category = dataFieldConfigBO.getCategory();
        PageCategoryEnum pageCategoryEnum = PageCategoryEnum.getEnum(category);
        DataUtils.trueThrowException(pageCategoryEnum == null, "页面类型不能为空!");
        if (PageCategoryEnum.CUSTOM.equals(pageCategoryEnum)) {
            dataFieldConfigBO.setType(PageTypeEnum.FUNCTION.getValue());
        } else {
            dataFieldConfigBO.setType(PageTypeEnum.SYSTEM.getValue());
        }
        pageBaseParam.setId(id)
                .setPlatform(platform)
                .setCategory(category)
                .setIndustry(StringUtils.isEmpty(pageBaseParam.getIndustry())? industry:pageBaseParam.getIndustry())
                .setShopId(shopId)
                .setTitle(dataFieldConfigBO.getTitle())
                .setDescription(dataFieldConfigBO.getDescription())
                .setHasTimingPublish(dataFieldConfigBO.getHasTimingPublish())
                .setGmtStart(dataFieldConfigBO.getGmtStart())
                .setType(dataFieldConfigBO.getType())
                ;

        if (id == null) {
            //校验页面唯一性
            Boolean canAdd = pageBaseServiceImpl.canAddByPageCategory(pageCategoryEnum);
            DataUtils.trueThrowException(!canAdd, "已经添加过该页面,禁止重复添加!");
            id = pageBaseServiceImpl.add(pageBaseParam);
        } else {
            PageBaseBO pageBaseBOOld = pageBaseServiceImpl.selById(id);
            DataUtils.trueThrowException(pageBaseBOOld == null, "当前页面不存在!");
            DataUtils.trueThrowException(!Objects.equals(pageBaseBOOld.getCategory(), pageBaseParam.getCategory()), "禁止修改页面类型!");
            id = pageBaseServiceImpl.modify(pageBaseParam);
        }
        List<PageModuleDO> dooList = CollUtil.newArrayList();
        for (int i = 0; i < moduleList.size(); i++) {
            PageModuleParam pageModuleParam = moduleList.get(i);
            pageModuleParam.setSort(i + 1);
            Long pageModuleId = pageModuleParam.getId();
            if (pageModuleId == null) {
                pageModuleId = GenerateId.generateId();
            }
            JSONObject dataFieldJsonObject = JsonUtils.fromJson(pageModuleParam.getDataField(), JSONObject.class);
            dataFieldJsonObject.put(Constant.FIELD_ID, pageModuleId.toString());
            if(StringUtils.isEmpty(pageModuleParam.getIndustry())){
                pageModuleParam.setIndustry(industry);
            }
            pageModuleParam.setDataField(JsonUtils.toJsonStr(dataFieldJsonObject));
            dooList.add(BeanUtil.convertBean(pageModuleParam, PageModuleDO.class)
                    .setId(pageModuleId)
                    .setPageId(id)
                    .setCreateBy(userId)
                    .setCreateTime(LocalDateTime.now())
                    .setModifyTime(LocalDateTime.now()));
        }
        getBean().delByPageId(id);
        getBean().insertBatch(dooList);
        return getBean().viewByPageId(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean publish(Long id){
        List<PageModuleDraftDO> dooDraftList = pageModuleDraftMapper.selectList(new LambdaQueryWrapper<PageModuleDraftDO>().eq(PageModuleDraftDO::getPageId, id));
        if(Collections3.isEmpty(dooDraftList)) throw  new BusinessException("没有可发布的内容");
        List<PageModuleDO> dooList = JsonUtils.jsonToList(dooDraftList, PageModuleDO.class);
        if(pageModuleMapper.selectCount(new LambdaQueryWrapper<PageModuleDO>().eq(PageModuleDO::getPageId, id)) >0){
            pageModuleMapper.delete(new LambdaQueryWrapper<PageModuleDO>().eq(PageModuleDO::getPageId, id));
        }
        if(pageModuleMapper.insertBatchSomeColumn(dooList) >0){
            LambdaQueryWrapper<PageModuleDraftDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(PageModuleDraftDO::getPageId, id);
            PageModuleDraftDO draftDO = new PageModuleDraftDO();
            draftDO.setPublishStatus(PublishStatusEnum.ENABLE.getValue());
            draftDO.setModifyTime(LocalDateTime.now());
            return pageModuleDraftMapper.update(draftDO, queryWrapper) >0;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertBatch(List<PageModuleDO> dooList) {
        dooList.stream().forEach(d->{
            if(StringUtils.isEmpty(d.getIndustry())){
                d.setIndustry(UserUtils.getTenantIndustry());
            }
            d.setCreateBy(UserUtils.getUserId());
            d.setCreateTime(LocalDateTime.now());
            d.setModifyTime(LocalDateTime.now());
        });
        pageModuleMapper.insertBatchSomeColumn(dooList);
        DataUtils.falseThrowException(cmsFeignCClient.removeCacheByIds(dooList.stream().map(PageModuleDO::getId).toArray(Long[]::new)).getData(), "操作失败!");
        return true;
    }

    @Override
    public PageModuleAllView viewByPageId(Long pageId) {
        PageBaseBO pageBaseBO = pageBaseServiceImpl.selById(pageId);
        return getBean().baseView(pageBaseBO);
    }

    @Override
    public PageModuleAllView viewByPageCategory(PageCategoryEnum pageCategoryEnum) {
        PageBaseBO pageBaseBO = pageBaseServiceImpl.selOneByPageCategory(pageCategoryEnum);
        if (pageBaseBO == null) {
            return null;
        }
        return getBean().baseView(pageBaseBO);
    }

    @Override
    public PageModuleAllView baseView(PageBaseBO pageBaseBO) {
        if (pageBaseBO == null) {
            throw BusinessException.create("当前页面数据不存在!");
        }
        Long pageId = pageBaseBO.getId();
        List<PageModuleDO> pageModuleDOS = getBean().selByPageId(pageId);
        PageModuleAllView pageModuleAllView = BeanUtil.convertBean(pageBaseBO, PageModuleAllView.class);
        List<PageModuleView> moduleList = pageModuleDOS.stream().map(x -> {
            //获取页面标题
            if (PageModuleTypeEnum.CONFIG.getValue().equalsIgnoreCase(x.getType())) {
                JSONObject jsonObject = JsonUtils.fromJson(x.getDataField(), JSONObject.class);
                pageModuleAllView.setTitle(Optional.ofNullable(jsonObject).map(obj -> obj.get(Constant.FIELD_TITLE)).map(obj -> obj.toString()).orElse(com.ibeeking.nematos.utils.data.StringUtils.EMPTY));
            }
            PageModuleView pageModuleView = new PageModuleView();
            pageModuleView.setId(x.getId());
            pageModuleView.setPageId(x.getPageId());
            pageModuleView.setSort(x.getSort());
            pageModuleView.setType(x.getType());
            pageModuleView.setDataField(JsonUtils.fromJson(x.getDataField(), JSONObject.class));
            return pageModuleView;
        }).collect(Collectors.toList());
        pageModuleAllView.setModuleList(moduleList);
        return pageModuleAllView;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changeStatus(Long id, Integer status) {
        PageModuleDO pageModuleDO = new PageModuleDO();
        pageModuleDO.setId(id);
        pageModuleDO.setModifyTime(LocalDateTime.now());
        DataUtils.falseThrowException(cmsFeignCClient.removeCacheByIds(id).getData(), "操作失败!");
        return pageModuleMapper.updateById(pageModuleDO) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id) {
        if (id == null) {
            return false;
        }
        PageModuleDO pageModuleDO = pageModuleMapper.selectById(id);
        if (pageModuleDO == null) {
            return true;
        }
        //清除缓存
        DataUtils.falseThrowException(cmsFeignCClient.removeCacheByIds(id).getData(), "操作失败!");
        return pageModuleMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delDraftByPageId(Long pageId) {
        if (pageId == null) {
            return false;
        }
        PageModuleQuery pageModuleQuery = new PageModuleQuery();
        pageModuleQuery.setPageId(pageId);
        //清除缓存
        DataUtils.falseThrowException(cmsFeignCClient.removeCacheByIds(pageId).getData(), "操作失败!");
        return pageModuleDraftMapper.delete(new LambdaQueryWrapper<PageModuleDraftDO>().eq(PageModuleDraftDO::getPageId, pageId)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delByPageId(Long pageId) {
        if (pageId == null) {
            return false;
        }
        PageModuleQuery pageModuleQuery = new PageModuleQuery();
        pageModuleQuery.setPageId(pageId);
        //清除缓存
        DataUtils.falseThrowException(cmsFeignCClient.removeCacheByIds(pageId).getData(), "操作失败!");
        return pageModuleMapper.delete(new LambdaQueryWrapper<PageModuleDO>().eq(PageModuleDO::getPageId, pageId)) > 0;
    }

    @Override
    public List<PageModuleDO> selByPageId(Long pageId) {
        if (pageId == null) {
            return Collections3.emptyList();
        }
        return pageModuleMapper.selectList(new LambdaQueryWrapper<PageModuleDO>().eq(PageModuleDO::getPageId, pageId).orderByAsc(PageModuleDO::getSort));
    }

    @Override
    public List<PageModuleDraftDO> selDraftByPageId(Long pageId) {
        if (pageId == null) {
            return Collections3.emptyList();
        }
        return pageModuleDraftMapper.selectList(new LambdaQueryWrapper<PageModuleDraftDO>().eq(PageModuleDraftDO::getPageId, pageId).orderByAsc(PageModuleDraftDO::getSort));
    }

    @Override
    public PageModuleBO selById(Long id) {
        if (id == null) {
            return null;
        }
        return BeanUtil.convertBeanByJson(pageModuleMapper.selectById(id), PageModuleBO.class);
    }

    @Override
    public PageModuleView pageModuleData(Long id) {
        PageModuleBO pageModuleBO = getBean().selById(id);
        if (pageModuleBO == null) {
            return null;
        }
        PageModuleView pageModuleView = new PageModuleView();
        pageModuleView.setId(pageModuleBO.getId());
        pageModuleView.setPageId(pageModuleBO.getPageId());
        pageModuleView.setSort(pageModuleBO.getSort());
        pageModuleView.setType(pageModuleBO.getType());
        pageModuleView.setDataField(JsonUtils.fromJson(pageModuleBO.getDataField(), JSONObject.class));
        String type = pageModuleView.getType();
        JSONObject dataField = pageModuleView.getDataField();
        return getBean().pageModuleDataBase(pageModuleView, type, dataField);
    }

    @Override
    public PageModuleView pageModuleData(PageModuleDataQuery query) {
        PageModuleBO pageModuleBO = getBean().selById(query.getId());
        if (pageModuleBO == null) {
            return null;
        }
        PageModuleView pageModuleView = new PageModuleView();
        pageModuleView.setId(pageModuleBO.getId());
        pageModuleView.setPageId(pageModuleBO.getPageId());
        pageModuleView.setSort(pageModuleBO.getSort());
        pageModuleView.setType(pageModuleBO.getType());
        pageModuleView.setDataField(JsonUtils.fromJson(pageModuleBO.getDataField(), JSONObject.class));
        String type = pageModuleView.getType();
        JSONObject dataField = pageModuleView.getDataField();
        return getBean().pageModuleDataBase(pageModuleView, type, dataField);
    }

    @Override
    public PageModuleView pageModuleDataSelQuery(PageModuleDataSelQuery query) {
        return getBean().pageModuleDataBase(new PageModuleView(), query.getType(), query.getDataField());
    }

    @Override
    public PageModuleView pageModuleDataBase(PageModuleView pageModuleView, String type, JSONObject dataField) {
        List<String> msgs = Collections3.newArrayList();
        DataUtils.trueThrowException(com.ibeeking.nematos.utils.data.StringUtils.isBlank(type), "获取页面组件数据异常(组件类型为空)!");
        pageModuleView = pageModuleView == null ? new PageModuleView() : pageModuleView;
        if (pageModuleView.getDataField() == null) {
            pageModuleView.setDataField(dataField);
        }
        if (com.ibeeking.nematos.utils.data.StringUtils.isBlank(pageModuleView.getType())) {
            pageModuleView.setType(type);
        }
        if (dataField == null) {
            return pageModuleView;
        }
        Object id = dataField.get(Constant.FIELD_ID);
        if (pageModuleView.getId() == null) {
            pageModuleView.setId(id == null ? null : Long.valueOf(id.toString()));
        }
        List pageModuleDataCommonVOS = CollUtil.newArrayList();
        if (PageModuleTypeEnum.GOODS.getValue().equalsIgnoreCase(type)) {
            //商品数据
            Object obj = dataField.get(PageModuleDataEnum.GOODS.getValue());
            List<Long> goodsIds = JsonUtils.jsonToList(obj, Long.class);
            if (Collections3.isEmpty(goodsIds)) {
                return pageModuleView;
            }
            ResponseResult<List<GoodsDTO>> responseResult = cmsGoodsFeignBClient.listByGoods(goodsIds);
            List<GoodsDTO> goodsDTOS = Optional.ofNullable(responseResult).map(ResponseResult::getData).orElse(Collections3.emptyList());
            msgs.add(DataUtils.methodMsg("cmsGoodsFeignBClient.listByGoods", goodsIds, responseResult));

            Map<Long, GoodsDTO> goodsDTOMap = goodsDTOS.stream().collect(Collectors.toMap(GoodsDTO::getId, x -> x, (o, n) -> n));
            goodsIds.forEach(goodsId -> {
                GoodsDTO goodsDTO = goodsDTOMap.get(goodsId);
                if (goodsDTO == null) {
                    return;
                }
                PageModuleDataCommonGoodsVO pageModuleDataCommonVO = new PageModuleDataCommonGoodsVO();
                pageModuleDataCommonVO.setId(goodsId);
                pageModuleDataCommonVO.setGoodsId(goodsId);
                pageModuleDataCommonVO.setName(goodsDTO.getName());
                pageModuleDataCommonVO.setImgUrl(goodsDTO.getThumb());
                pageModuleDataCommonVO.setPrice(NumberUtils.cent2Yuan(goodsDTO.getPrice()));
                pageModuleDataCommonVO.setStock(goodsDTO.getStock());
                pageModuleDataCommonVO.setSaleStock(goodsDTO.getSaleStock());
                pageModuleDataCommonVO.setMarkingPrice(NumberUtils.cent2Yuan(goodsDTO.getMarkingPrice()));
                pageModuleDataCommonVO.setChannel(GoodsChannelEnum.GOODS.getValue());
                pageModuleDataCommonVO.setGoodsTagsNames(goodsDTO.getGoodsTagsNames());
                pageModuleDataCommonVOS.add(pageModuleDataCommonVO);
            });
        } else if (PageModuleTypeEnum.GOODS_CATEGORY_ROOT.getValue().equalsIgnoreCase(type)) {
            //商品类目数据
            ResponseResult<List<GoodsCategoryDTO>> responseResult = cmsGoodsFeignBClient.goodsCategoryEffectiveRootDtoList();
            List<GoodsCategoryDTO> goodsCategoryDTOS = Optional.ofNullable(responseResult).map(ResponseResult::getData).orElse(Collections3.emptyList());
            msgs.add(DataUtils.methodMsg("cmsGoodsFeignBClient.goodsCategoryEffectiveRootDtoList", null, responseResult));
            if (Collections3.isEmpty(goodsCategoryDTOS)) {
                return pageModuleView;
            }
            goodsCategoryDTOS.forEach(dto -> {
                PageModuleDataBaseVO pageModuleDataCommonVO = new PageModuleDataBaseVO();
                pageModuleDataCommonVO.setId(dto.getId());
                pageModuleDataCommonVO.setName(dto.getName());
                pageModuleDataCommonVO.setImgUrl(dto.getPic());
                pageModuleDataCommonVOS.add(pageModuleDataCommonVO);
            });
        } else if (PageModuleTypeEnum.POINTS_EXCHANGE.getValue().equalsIgnoreCase(type)) {
            //获取活动
            Object obj = dataField.get(PageModuleDataEnum.ID_OBJ_S.getValue());
            List<CampaignLowPriceFeignQuery> paramList = JsonUtils.jsonToList(obj, CampaignLowPriceFeignQuery.class);
            if (Collections3.isEmpty(paramList)) {
                return pageModuleView;
            }
            //查询活动
            ResponseResult<List<CampaignLowPriceDTO>> listResponseResult = cmsMarketingFeignBClient.queryGoodsLowPrice(paramList);
            msgs.add(DataUtils.methodMsg("cmsMarketingFeignBClient.queryGoodsLowPrice", paramList, listResponseResult));
            List<CampaignLowPriceDTO> campaignListDTOS = Optional.ofNullable(listResponseResult.getData()).orElse(Collections3.emptyList());
            Map<Long, CampaignLowPriceDTO> dtoMap = campaignListDTOS.stream().collect(Collectors.toMap(CampaignLowPriceDTO::getId, x -> x, (o, n) -> n));

            //查询商品
            List<Long> goodsIds = campaignListDTOS.stream().map(CampaignLowPriceDTO::getUnionId).collect(Collectors.toList());
            ResponseResult<List<GoodsDTO>> responseResult = cmsGoodsFeignBClient.listByGoods(goodsIds);
            List<GoodsDTO> goodsDTOS = Optional.ofNullable(responseResult).map(ResponseResult::getData).orElse(Collections3.emptyList());
            msgs.add(DataUtils.methodMsg("cmsGoodsFeignBClient.listByGoods", goodsIds, responseResult));
            Map<Long, GoodsDTO> goodsDTOMap = goodsDTOS.stream().collect(Collectors.toMap(GoodsDTO::getId, x -> x, (o, n) -> n));

            paramList.forEach(x -> {
                CampaignLowPriceDTO campaignLowPriceDTO = dtoMap.get(x.getCampaignId());
                GoodsDTO goodsDTO = goodsDTOMap.get(x.getGoodsId());
                if (campaignLowPriceDTO == null && goodsDTO == null) {
                    return;
                }
                PageModuleDataCommonPointsExchangeVO pageModuleDataCommonVO = new PageModuleDataCommonPointsExchangeVO();
                pageModuleDataCommonVO.setChannel(x.getChannel());
                pageModuleDataCommonVO.setId(campaignLowPriceDTO.getId());
                pageModuleDataCommonVO.setImgUrl(goodsDTO.getThumb());
                pageModuleDataCommonVO.setName(goodsDTO.getName());
                pageModuleDataCommonVO.setGoodsId(campaignLowPriceDTO.getUnionId());
                pageModuleDataCommonVO.setSkuId(campaignLowPriceDTO.getGoodsSkuId());
                pageModuleDataCommonVO.setPrice(NumberUtils.cent2Yuan(campaignLowPriceDTO.getPrice()));
                pageModuleDataCommonVO.setMarkingPrice(NumberUtils.cent2Yuan(goodsDTO.getMarkingPrice()));
                pageModuleDataCommonVO.setPoints(campaignLowPriceDTO.getPoints());
                pageModuleDataCommonVO.setStock(goodsDTO.getStock());
                pageModuleDataCommonVO.setSaleStock(goodsDTO.getSaleStock());

                pageModuleDataCommonVO.setActivityId(campaignLowPriceDTO.getId());
                pageModuleDataCommonVO.setActivityName(campaignLowPriceDTO.getName());
                pageModuleDataCommonVO.setStatus(campaignLowPriceDTO.getCampaignStatus());
                pageModuleDataCommonVO.setStatusDesc(campaignLowPriceDTO.getCampaignStatusName());
                pageModuleDataCommonVO.setActivityType(campaignLowPriceDTO.getSceneCode());
                pageModuleDataCommonVO.setActivityTypeName(campaignLowPriceDTO.getSceneName());
                pageModuleDataCommonVO.setActivityValidityStart(campaignLowPriceDTO.getStartTime());
                pageModuleDataCommonVO.setActivityValidityEnd(campaignLowPriceDTO.getEndTime());
                pageModuleDataCommonVO.setActivityLeftTime(campaignLowPriceDTO.getDistanceTimeMillis());
                pageModuleDataCommonVOS.add(pageModuleDataCommonVO);
            });
        } else if (PageModuleTypeEnum.IMAGE_AD.getValue().equalsIgnoreCase(type)) {
            Object obj = dataField.get(PageModuleDataEnum.IMAGE_AD.getValue());
            List<ImageListParam> paramList = JsonUtils.jsonToList(obj, ImageListParam.class);
            if (Collections3.isEmpty(paramList)) {
                return pageModuleView;
            }
            //推广活动 要展示报名状态
            Map<Long, CampaignInfoDTO> promotionCampaignMap = new HashMap<>();
            List<Long> promotionCampaignIds = paramList.stream().filter(x -> CampaignSceneEnum.PROMOTION_SCENE.getChannel().equals(x.getChannel())).map(ImageListParam::getActivityId).collect(Collectors.toList());
            if (Collections3.isNotEmpty(promotionCampaignIds)) {
                ResponseResult<List<CampaignInfoDTO>> promotionCampaignResult = cmsMarketingFeignCClient.queryCampaignInfoDTO(new FeignCampaignQuery().setCampaignIds(promotionCampaignIds).setCheckStatus(CheckStatusEnum.CHECKED.getCode()).setCampaignStatusList(Collections3.newArrayList(CampaignStatusEnum.NOT_STARTED.getCode(), CampaignStatusEnum.IN_PROGRESS.getCode(), CampaignStatusEnum.EXPIRED.getCode())));
                List<CampaignInfoDTO> promotionCampaigns = promotionCampaignResult.getData();
                if (Collections3.isNotEmpty(promotionCampaigns)) {
                    promotionCampaignMap = promotionCampaigns.stream().collect(Collectors.toMap(CampaignInfoDTO::getId, x -> x));
                }
            }
            Map<Long, CampaignInfoDTO> promotionCampaignMapFinal = promotionCampaignMap;
            List<PageModuleDataCommonImageAdVO> voList = JsonUtils.jsonToList(obj, PageModuleDataCommonImageAdVO.class);
            voList.forEach(pageModuleDataCommonVO -> {
                pageModuleDataCommonVO.setId(pageModuleDataCommonVO.getId());
                pageModuleDataCommonVO.setImgUrl(pageModuleDataCommonVO.getUrl());
                pageModuleDataCommonVO.setName(pageModuleDataCommonVO.getTitle());
                pageModuleDataCommonVO.setType(pageModuleDataCommonVO.getType());
                pageModuleDataCommonVO.setLink(pageModuleDataCommonVO.getLink());
                pageModuleDataCommonVO.setChannel(pageModuleDataCommonVO.getChannel());
                if (PageTypeEnum.GOODS.getValue().equals(pageModuleDataCommonVO.getType())) {
                    pageModuleDataCommonVO.setGoodsId(Long.valueOf(pageModuleDataCommonVO.getLink()));
                    pageModuleDataCommonVO.setId(pageModuleDataCommonVO.getGoodsId());
                    pageModuleDataCommonVO.setChannel(GoodsChannelEnum.GOODS.getValue());
                } else if (PageTypeEnum.ACTIVITY.getValue().equals(pageModuleDataCommonVO.getType())) {
                    pageModuleDataCommonVO.setActivityId(pageModuleDataCommonVO.getActivityId());
                    pageModuleDataCommonVO.setGoodsId(pageModuleDataCommonVO.getGoodsId());
                    pageModuleDataCommonVO.setChannel(pageModuleDataCommonVO.getChannel());
                    if (promotionCampaignMapFinal.get(pageModuleDataCommonVO.getActivityId()) != null) {
                        pageModuleDataCommonVO.setIsTag(true);
                        pageModuleDataCommonVO.setLabelName(promotionCampaignMapFinal.get(pageModuleDataCommonVO.getActivityId()).getEnrollStatusName());
                    }
                }
                pageModuleDataCommonVOS.add(pageModuleDataCommonVO);
            });
        } else if (PageModuleTypeEnum.MARKETING_COUPON.getValue().equalsIgnoreCase(type)) {
            //优惠券添加方式 0手动 1自动
            Integer addType = Integer.valueOf(Objects.toString(dataField.get("addType")));
            //失效或抢完券 是否展示
            Integer invalid = Integer.valueOf(Objects.toString(dataField.get("invalid")));
            if (addType == 0) {
                //0手动 获取选中的券列表
                List<CouponParam> couponList = JsonUtils.jsonToList(dataField.get(PageModuleDataEnum.MARKETING_COUPON_MANUAL.getValue()), CouponParam.class);
                if (Collections3.isNotEmpty(couponList)) {
                    List<Long> couponIdList = couponList.stream().map(CouponParam::getId).collect(Collectors.toList());
                    ResponseResult<List<CouponInfoDTO>> listResponseResult = cmsMarketingFeignBClient.queryInfoByIds(couponIdList);
                    DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(listResponseResult.getCode()), "系统异常,未查询到券信息!");
                    List<CouponInfoDTO> couponInfoList = listResponseResult.getData();
                    if (Collections3.isNotEmpty(couponInfoList)) {
                        Map<Long, CouponInfoDTO> couponInfoDTOMap = couponInfoList.stream().collect(Collectors.toMap(CouponInfoDTO::getId, x -> x, (o, n) -> n));
                        PageModuleDataCouponVO pageModuleDataCouponVO;
                        for (Long couponId : couponIdList) {
                            CouponInfoDTO couponInfoDTO = couponInfoDTOMap.get(couponId);
                            if (couponInfoDTO != null) {
                                pageModuleDataCouponVO = JsonUtils.fromJson(couponInfoDTO, PageModuleDataCouponVO.class);
                                pageModuleDataCouponVO.setNumber(com.ibeeking.nematos.utils.data.DataUtils.fromFenToYuan(couponInfoDTO.getWorth(), 2, ROUND_UNNECESSARY));
                                pageModuleDataCouponVO.setDesc(couponInfoDTO.getNameRemark());
                                pageModuleDataCommonVOS.add(pageModuleDataCouponVO);
                            }
                        }
                    }
                }
            } else {
                // 1自动获取券
                CouponAutoParam couponAutoParam = JsonUtils.fromJson(dataField.get(PageModuleDataEnum.MARKETING_COUPON_AUTO.getValue()), CouponAutoParam.class);
                if (couponAutoParam != null) {
                    CouponInfoPageQuery couponInfoPageQuery = JsonUtils.fromJson(couponAutoParam, CouponInfoPageQuery.class);
                    couponInfoPageQuery.setPageNum(1);
                    couponInfoPageQuery.setPageSize(couponAutoParam.getShowMaxQty());
                    couponInfoPageQuery.setEffective(true);
                    //失效或抢完券 是否展示
                    if (invalid == 0) {
                        //todo 抢完券的情况
                    }
                    ResponseResult<com.ibeeking.nematos.utils.base.Page<CouponInfoPageDTO>> listResponseResult = cmsMarketingFeignBClient.couponPageList(couponInfoPageQuery);
                    DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(listResponseResult.getCode()), "系统异常,未查询到券信息!");
                    List<CouponInfoPageDTO> couponInfoList = Optional.ofNullable(listResponseResult.getData()).map(x -> x.getRecords()).orElse(null);
                    if (Collections3.isNotEmpty(couponInfoList)) {
                        PageModuleDataCouponVO pageModuleDataCouponVO;
                        for (CouponInfoPageDTO couponInfoDTO : couponInfoList) {
                            pageModuleDataCouponVO = JsonUtils.fromJson(couponInfoDTO, PageModuleDataCouponVO.class);
                            pageModuleDataCouponVO.setNumber(couponInfoDTO.getWorthNum());
                            pageModuleDataCouponVO.setDesc(couponInfoDTO.getNameRemark());
                            pageModuleDataCommonVOS.add(pageModuleDataCouponVO);
                        }
                    }
                }
            }
        } else if (PageModuleTypeEnum.GOODS_GROUP.getValue().equalsIgnoreCase(type)) {
            Object obj = dataField.get(PageModuleDataEnum.GOODS_GROUP.getValue());
            List<GoodGroupParam> paramList = JsonUtils.jsonToList(obj, GoodGroupParam.class);
            if (Collections3.isEmpty(paramList)) {
                return pageModuleView;
            }
            List<Long> groupIds = paramList.stream().map(GoodGroupParam::getId).collect(Collectors.toList());
            ResponseResult<List<GoodsGroupBaseDTO>> listResponseResult = cmsGoodsFeignBClient.queryGoodsGroupByIds(groupIds);
            DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(listResponseResult.getCode()), "系统异常,未查询到商品分组!");
            List<GoodsGroupBaseDTO> goodsGroupList = listResponseResult.getData();
            Map<Long, String> map = goodsGroupList.stream().collect(Collectors.toMap(GoodsGroupBaseDTO::getId, GoodsGroupBaseDTO::getName, (o, n) -> n));
            paramList.forEach(x -> {
                PageModuleDataGoodsGroupVO pageModuleDataGoodsGroupVO = JsonUtils.fromJson(x, PageModuleDataGoodsGroupVO.class);
                pageModuleDataGoodsGroupVO.setId(x.getId());
                pageModuleDataGoodsGroupVO.setName(x.getGroupName());
                pageModuleDataGoodsGroupVO.setSource(map.get(x.getId()));
                pageModuleDataCommonVOS.add(pageModuleDataGoodsGroupVO);
            });
        } else if (PageModuleTypeEnum.SHOP.getValue().equalsIgnoreCase(type)) {
            Object obj = dataField.get("shopId");
            DataUtils.trueThrowException(obj == null, "无法获取店铺信息!");
            Long shopId = Long.valueOf(obj.toString());
            ResponseResult<CmsShopModuleInfoDTO> cmsShopModuleInfoDTOResponseResult = cmsSystemCClient.cmsShopModuleInfo(shopId);
            CmsShopModuleInfoDTO data = cmsShopModuleInfoDTOResponseResult.getData();
            DataUtils.trueThrowException(data == null, "获取店铺信息失败!");
            ResponseResult<Integer> shopGoodsQtyRes = cmsGoodsFeignCClient.queryShopGoodsQty(new ShopGoodsQtyQuery().setShopId(shopId).setGoodsChannel(GoodsChannelEnum.GOODS.getValue()).setGoodsStatus(GoodsStatusEnum.ON_SHELF.getValue()));
            PageModuleDataShopVO pageModuleDataShopVO = BeanUtil.convertBean(data, PageModuleDataShopVO.class);
            pageModuleDataShopVO.setOldShopName(data.getName());
            String name = Objects.toString(dataField.get("name"));
            pageModuleDataShopVO.setName(com.ibeeking.nematos.utils.data.StringUtils.isBlank(name) ? data.getName() : name);
            pageModuleDataShopVO.setGoodsTotalQty(shopGoodsQtyRes.getData());
            pageModuleDataShopVO.setImgUrl(pageModuleDataShopVO.getLogo());
            pageModuleDataCommonVOS.add(pageModuleDataShopVO);
        } else if (PageModuleTypeEnum.SECKILL.getValue().equalsIgnoreCase(type)) {
            List<CampaignSeckillRoundParam> campaignSeckillRoundList = JsonUtils.jsonToList(dataField.get(PageModuleDataEnum.MARKETING_SECKILL_ROUND_LIST.getValue()), CampaignSeckillRoundParam.class);
            if (Collections3.isNotEmpty(campaignSeckillRoundList)) {
                List<PageModuleDataCommonSeckillRoundVO> pageModuleDataCommonSeckillRoundVOS = JsonUtils.jsonToList(campaignSeckillRoundList, PageModuleDataCommonSeckillRoundVO.class);
                String today = DateUtil.today();
                for (PageModuleDataCommonSeckillRoundVO seckillRoundVO : pageModuleDataCommonSeckillRoundVOS) {
                    String seckillRoundTodayStartTime = today + seckillRoundVO.getStartTimePeriod();
                    String seckillRoundTodayEndTime = today + seckillRoundVO.getEndTimePeriod();
                    //针对秒杀处理时间
                    if (LocalDateTime.now().toString().compareTo(seckillRoundTodayStartTime) < 0) {
                        seckillRoundVO.setStatus(CampaignStatusEnum.NOT_STARTED.getCode());
                        seckillRoundVO.setStatusDesc(CampaignStatusEnum.NOT_STARTED.getLable());
                        //活动未开始 计算距离活动开始时间的毫秒值
                        seckillRoundVO.setLeftTime(DateUtil.betweenMs(Calendar.getInstance().getTime(), DateUtils.parseDate(seckillRoundTodayStartTime)));
                    } else if (LocalDateTime.now().toString().compareTo(seckillRoundTodayStartTime) >= 0 && LocalDateTime.now().toString().compareTo(seckillRoundTodayEndTime) <= 0) {
                        seckillRoundVO.setStatus(CampaignStatusEnum.IN_PROGRESS.getCode());
                        seckillRoundVO.setStatusDesc(CampaignStatusEnum.IN_PROGRESS.getLable());
                        //活动进行中 计算距离活动结束时间的毫秒值
                        seckillRoundVO.setLeftTime(DateUtil.betweenMs(Calendar.getInstance().getTime(), DateUtils.parseDate(seckillRoundTodayEndTime)));
                    } else if (LocalDateTime.now().toString().compareTo(seckillRoundTodayEndTime) > 0) {
                        seckillRoundVO.setStatus(CampaignStatusEnum.EXPIRED.getCode());
                        seckillRoundVO.setStatusDesc(CampaignStatusEnum.EXPIRED.getLable());
                    }
                }
                pageModuleDataCommonVOS.addAll(pageModuleDataCommonSeckillRoundVOS);
            }
        } else if (PageModuleTypeEnum.ARTICLE.getValue().equalsIgnoreCase(type)) {
            ArticleSourceParam articleSourceParam = JsonUtils.fromJson(dataField.get(PageModuleDataEnum.ARTICLE_SOURCE.getValue()), ArticleSourceParam.class);
            if (articleSourceParam != null) {
                int articleSource = articleSourceParam.getArticleSource();
                if (ArticleSourceParam.ARTICLE_SOURCE_ARTICLE_LIST == articleSource) {
                    ArticleQuery articleQuery = new ArticleQuery();
                    if (CollUtil.isNotEmpty(articleSourceParam.getArticleIdList())) {
                        //文章来源方式-文章列表
                        articleQuery.setIds(articleSourceParam.getArticleIdList());
                        ResponseResult<List<ArticleDTO>> articleListResult = cmsMarketingFeignCClient.articleList(articleQuery);
                        DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(articleListResult.getCode()), "未查询到文章信息");
                        List<PageModuleDataCommonArticleVO> pageModuleDataCommonArticleVOS = JsonUtils.jsonToList(articleListResult.getData(), PageModuleDataCommonArticleVO.class);
                        for (PageModuleDataCommonArticleVO vo : pageModuleDataCommonArticleVOS) {
                            vo.setName(vo.getTitle());
                            if (Collections3.isNotEmpty(vo.getCoverImgList())) {
                                vo.setImgUrl(vo.getCoverImgList().get(0));
                            }
                        }
                        pageModuleDataCommonVOS.addAll(pageModuleDataCommonArticleVOS);
                    }
                } else {
                    if (CollUtil.isNotEmpty(articleSourceParam.getGroupIdList())) {
                        ArticleGroupQuery articleGroupQuery = new ArticleGroupQuery().setIds(articleSourceParam.getGroupIdList());
                        ResponseResult<List<ArticleGroupDTO>> articleGroupListResult = cmsMarketingFeignCClient.articleGroupList(articleGroupQuery);
                        DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(articleGroupListResult.getCode()), "未查询到文章信息");
                        List<PageModuleDataBaseVO> pageModuleDataBaseVOS = JsonUtils.jsonToList(articleGroupListResult.getData(), PageModuleDataBaseVO.class);
                        pageModuleDataCommonVOS.addAll(pageModuleDataBaseVOS);
                    }
                }
            }
        }
        pageModuleView.setItems(pageModuleDataCommonVOS);
        pageModuleView.setMsgs(msgs);
        return pageModuleView;
    }

    @Override
    public List pageModuleChildData(PageModuleChildDataQuery query) {
        PageModuleBO pageModuleBO = getBean().selById(query.getId());
        if (pageModuleBO == null) {
            return Collections3.emptyList();
        }
        JSONObject jsonObject = JsonUtils.fromJson(pageModuleBO.getDataField(), JSONObject.class);
        String type = pageModuleBO.getType();
        String childId = query.getChildId();
        return getBean().pageModuleChildDataBase(childId, type, jsonObject);
    }

    @Override
    public List pageModuleChildDataSelQuery(PageModuleChildDataSelQuery query) {
        return getBean().pageModuleChildDataBase(query.getChildId(), query.getType(), query.getDataField());
    }

    @Override
    public List pageModuleChildDataBase(String childId, String type, JSONObject dataField) {
        if (PageModuleTypeEnum.GOODS_GROUP.getValue().equalsIgnoreCase(type)) {
            Object obj = dataField.get(PageModuleDataEnum.GOODS_GROUP.getValue());
            List<GoodGroupParam> paramList = JsonUtils.jsonToList(obj, GoodGroupParam.class);
            if (Collections3.isEmpty(paramList)) {
                return Collections3.emptyList();
            }
            GoodGroupParam goodGroupParam = paramList.stream().filter(x -> childId.equals(Objects.toString(x.getId()))).findFirst().orElse(null);
            if (goodGroupParam == null) {
                return Collections3.emptyList();
            }
            Integer goodsNumType = goodGroupParam.getGoodsNumType();
            GoodsByGroupQuery goodsByGroupQuery = new GoodsByGroupQuery();
            goodsByGroupQuery.setGoodsGroupId(Long.valueOf(childId));
            goodsByGroupQuery.setGoodsNum(goodsNumType == 2 ? null : goodGroupParam.getGoodsNum());
            goodsByGroupQuery.setChannels(Arrays.asList(GoodsChannelEnum.GOODS.getValue()));
            goodsByGroupQuery.setStatus(GoodsStatusEnum.ON_SHELF.getValue());
            ResponseResult<List<GoodsVoDTO>> listResponseResult = cmsGoodsFeignBClient.queryGoodsByGroup(goodsByGroupQuery);
            DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(listResponseResult.getCode()), "系统异常,未查询到商品分组下的商品!");
            List<GoodsVoDTO> dataList = listResponseResult.getData();
            List<PageModuleChildDataGoodsGroupVO> pageModuleChildDataGoodsGroupVOS = new ArrayList<>();
            PageModuleChildDataGoodsGroupVO vo;
            for (GoodsVoDTO goodsVoDTO : dataList) {
                vo = JsonUtils.fromJson(goodsVoDTO, PageModuleChildDataGoodsGroupVO.class);
                if (vo != null) {
                    vo.setId(goodsVoDTO.getId());
                    vo.setGoodsId(goodsVoDTO.getId());
                    vo.setSkuId(goodsVoDTO.getDefaultSkuId());
                    vo.setImgUrl(goodsVoDTO.getThumb());
                    vo.setStock(goodsVoDTO.getEffectiveStock());
                    vo.setChannel(GoodsChannelEnum.GOODS.getValue());
                    String goodsTagsNames = goodsVoDTO.getGoodsTagsNames();
                    if (StringUtils.isNotBlank(goodsTagsNames)) {
                        String[] goodsTagsNameArr = goodsTagsNames.split(",");
                        vo.setGoodsTagsNameList(CollUtil.newArrayList(goodsTagsNameArr));
                    } else {
                        vo.setGoodsTagsNameList(Collections3.emptyList());
                    }
                    pageModuleChildDataGoodsGroupVOS.add(vo);
                }
            }
            return pageModuleChildDataGoodsGroupVOS;
        } else if (PageModuleTypeEnum.ARTICLE.getValue().equalsIgnoreCase(type)) {
            ArticleSourceParam articleSourceParam = JsonUtils.fromJson(dataField.get(PageModuleDataEnum.ARTICLE_SOURCE.getValue()), ArticleSourceParam.class);
            if (articleSourceParam != null) {
                int articleSource = articleSourceParam.getArticleSource();
                if (ArticleSourceParam.ARTICLE_SOURCE_GROUP_LIST == articleSource) {
                    ArticleQuery articleQuery = new ArticleQuery();
                    //文章来源方式-文章列表
                    articleQuery.setGroupId(Long.valueOf(childId));
                    articleQuery.setMaxNum(articleSourceParam.getMaxArticleNum());
                    ResponseResult<List<ArticleDTO>> articleListResult = cmsMarketingFeignCClient.articleList(articleQuery);
                    DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(articleListResult.getCode()), "未查询到文章信息");
                    List<PageModuleDataCommonArticleVO> pageModuleDataCommonArticleVOS = JsonUtils.jsonToList(articleListResult.getData(), PageModuleDataCommonArticleVO.class);
                    for (PageModuleDataCommonArticleVO vo : pageModuleDataCommonArticleVOS) {
                        vo.setName(vo.getTitle());
                        if (Collections3.isNotEmpty(vo.getCoverImgList())) {
                            vo.setImgUrl(vo.getCoverImgList().get(0));
                        }
                    }
                    return pageModuleDataCommonArticleVOS;
                }
            }
        }
        return Collections3.emptyList();
    }

    @Override
    public Page pageModuleChildDataPageList(PageModuleChildDataPageListQuery query) {
        Page pageRes = new Page(query.getPageNum(), query.getPageSize());
        String type = query.getType();
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        Object childDataParam = query.getChildDataParam();
        if (PageModuleTypeEnum.SECKILL.getValue().equalsIgnoreCase(type)) {
            CampaignSeckillRoundParam campaignSeckillRoundParam = JsonUtils.fromJson(childDataParam, CampaignSeckillRoundParam.class);
            ParamCheckUtils.check(campaignSeckillRoundParam);
            Integer addType = campaignSeckillRoundParam.getAddType();
            CampaignPageListQuery campaignPageListQuery = new CampaignPageListQuery();
            campaignPageListQuery.setPageNum(pageNum);
            campaignPageListQuery.setPageSize(pageSize);
            campaignPageListQuery.setCampaignScene(CampaignSceneEnum.SECKILL_SCENE.name());
            campaignPageListQuery.setCheckStatus(CheckStatusEnum.CHECKED.getCode());
            String startTimePeriod = campaignSeckillRoundParam.getStartTimePeriod();
            String endTimePeriod = campaignSeckillRoundParam.getEndTimePeriod();
            String startTime = campaignSeckillRoundParam.getStartTime();
            String endTime = campaignSeckillRoundParam.getEndTime();
            String roundStartTime = startTime + " " + startTimePeriod;
            String roundEndTime = endTime + " " + endTimePeriod;
            campaignPageListQuery.setStartTime(roundStartTime);
            campaignPageListQuery.setEndTime(roundEndTime);
            if (addType == 0) {
                //手动获取
                List<CampaignSeckillManualParam> campaignManualList = campaignSeckillRoundParam.getCampaignManualList();
                if (Collections3.isEmpty(campaignManualList)) {
                    return new Page();
                }
                campaignPageListQuery.setCampaignIds(campaignManualList.stream().map(CampaignSeckillManualParam::getActivityId).collect(Collectors.toList()));
            } else {
                //自动获取
                CampaignSeckillAutoConfigParam campaignAutoConfig = campaignSeckillRoundParam.getCampaignAutoConfig();
                Integer autoType = campaignAutoConfig.getType();
                if (autoType == 0) {
                    //查询满足条件的全部活动
                    campaignPageListQuery.setCampaignStatusList(Arrays.asList(CampaignStatusEnum.NOT_STARTED.getCode(), CampaignStatusEnum.IN_PROGRESS.getCode()));
                } else {
                    String campaignStartTime = DateUtils.dayStartStr(campaignAutoConfig.getCampaignStartTime());
                    String campaignEndTime = DateUtils.dayEndStr(campaignAutoConfig.getCampaignEndTime());
                    if (StringUtils.isNotBlank(campaignStartTime) && campaignStartTime.compareTo(roundStartTime) > 0) {
                        campaignPageListQuery.setEndTime(campaignStartTime);
                    }
                    if (StringUtils.isNotBlank(campaignEndTime) && campaignEndTime.compareTo(roundEndTime) < 0) {
                        campaignPageListQuery.setEndTime(campaignEndTime);
                    }
                    campaignPageListQuery.setCampaignStatusList(campaignAutoConfig.getStatusList());
                }
            }
            ResponseResult<com.ibeeking.nematos.utils.base.Page<CampaignPageDTO>> pageResponseResult = cmsMarketingFeignBClient.queryCampaignPageList(campaignPageListQuery);
            DataUtils.falseThrowException(ResponseCodeEnum.SUCCESS.getCode().equals(pageResponseResult.getCode()), "系统异常,未查询到秒杀活动!");
            com.ibeeking.nematos.utils.base.Page<CampaignPageDTO> data = pageResponseResult.getData();
            Page<CampaignPageDTO> page = JsonUtils.fromJson(data, Page.class);
            List<CampaignPageDTO> records = JsonUtils.jsonToList(page.getRecords(), CampaignPageDTO.class);
            if (Collections3.isEmpty(records)) {
                return pageRes;
            }
            //查询商品
            List<Long> goodsIds = records.stream().filter(x -> x.getLowPriceCampaignGoodsVO() != null).map(x -> x.getLowPriceCampaignGoodsVO().getGoodsId()).collect(Collectors.toList());
            ResponseResult<List<GoodsDTO>> responseResult = cmsGoodsFeignBClient.listByGoods(goodsIds);
            List<GoodsDTO> goodsDTOS = Optional.ofNullable(responseResult).map(ResponseResult::getData).orElse(Collections3.emptyList());
            Map<Long, GoodsDTO> goodsDTOMap = goodsDTOS.stream().collect(Collectors.toMap(GoodsDTO::getId, x -> x, (o, n) -> n));

            List<PageModuleDataCommonSeckillVO> voList = new ArrayList<>();

            PageModuleDataCommonSeckillVO vo;
            for (CampaignPageDTO campaignPageDTO : records) {
                vo = new PageModuleDataCommonSeckillVO();
                vo.setId(campaignPageDTO.getId());
                vo.setActivityName(campaignPageDTO.getName());
                vo.setActivityId(campaignPageDTO.getId());
                vo.setActivityTypeName(CampaignSceneEnum.SECKILL_SCENE.getLable());
                vo.setChannel(campaignPageDTO.getChannel().get(0));
                Integer campaignStatus = campaignPageDTO.getCampaignStatus();
                vo.setStatus(campaignStatus);
                vo.setStatusDesc(campaignPageDTO.getCampaignStatusName());
                vo.setActivityValidityStart(campaignPageDTO.getStartTime());
                vo.setActivityValidityEnd(campaignPageDTO.getEndTime());
                //针对秒杀处理时间
                if (CampaignStatusEnum.NOT_STARTED.getCode().equals(campaignStatus)) {
                    //活动未开始 计算距离活动开始时间的毫秒值
                    vo.setActivityLeftTime(DateUtil.betweenMs(Calendar.getInstance().getTime(), DateUtils.parseDate(campaignPageDTO.getStartTime())));
                } else if (CampaignStatusEnum.IN_PROGRESS.getCode().equals(campaignStatus)) {
                    //活动进行中 计算距离活动场次/活动结束时间(取最近的)的毫秒值
                    String campaignEndTime = campaignPageDTO.getEndTime();
                    vo.setActivityLeftTime(DateUtil.betweenMs(Calendar.getInstance().getTime(), roundEndTime.compareTo(campaignEndTime) < 0 ? DateUtils.parseDate(roundEndTime) : DateUtils.parseDate(campaignEndTime)));
                }
                //商品活动库存
                Integer campaignGoodsDTOStock = Integer.valueOf(Optional.ofNullable(campaignPageDTO.getCampaignVO()).map(x -> x.get("effectiveStock")).orElse(0).toString());
                vo.setGoodsEffectiveStock(campaignGoodsDTOStock);
                CampaignGoodsDTO lowPriceCampaignGoodsVO = campaignPageDTO.getLowPriceCampaignGoodsVO();
                if (lowPriceCampaignGoodsVO != null) {
                    vo.setPrice(NumberUtils.cent2Yuan(lowPriceCampaignGoodsVO.getPrice()));
                    vo.setGoodsId(lowPriceCampaignGoodsVO.getGoodsId());
                    vo.setSkuId(lowPriceCampaignGoodsVO.getGoodsSkuId());
                    GoodsDTO goodsDTO = goodsDTOMap.get(lowPriceCampaignGoodsVO.getGoodsId());
                    if (goodsDTO != null) {
                        vo.setName(goodsDTO.getName());
                        vo.setRemark(goodsDTO.getRemark());
                        vo.setImgUrl(goodsDTO.getThumb());
                        vo.setMarkingPrice(NumberUtils.cent2Yuan(goodsDTO.getPrice()));
                        vo.setSaleStock(goodsDTO.getSaleStock());
                        //优惠额
                        BigDecimal preferentialPrice = NumberUtil.sub(vo.getMarkingPrice(), vo.getPrice());
                        vo.setPreferentialPrice(NumberUtil.isGreater(preferentialPrice, BigDecimal.ZERO) ? preferentialPrice : BigDecimal.ZERO);
                        if (goodsDTO.getStock() < campaignGoodsDTOStock) {
                            //商品活动库存,商品库存取小的值
                            vo.setGoodsEffectiveStock(goodsDTO.getStock());
                        }
                    }
                }
                vo.setStock(vo.getGoodsEffectiveStock());
                voList.add(vo);
            }
            pageRes = JsonUtils.fromJson(page, Page.class);
            pageRes.setRecords(voList);
        }
        return pageRes;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addList(List<PageModuleParam> paramList) {
        if (Collections3.isEmpty(paramList)) {
            return false;
        }
        List<PageModuleDO> pageModuleDOS = JsonUtils.jsonToList(paramList, PageModuleDO.class);
        pageModuleDOS.stream().forEach(d->{
            if(com.ibeeking.nematos.utils.data.StringUtils.isEmpty(d.getIndustry())){
                d.setIndustry(UserUtils.getTenantIndustry());
            }
            d.setCreateBy(UserUtils.getUserId());
            d.setCreateTime(LocalDateTime.now());
            d.setModifyTime(LocalDateTime.now());
        });
        return pageModuleMapper.insertBatchSomeColumn(pageModuleDOS) > 0;
    }

    private PageModuleServiceImpl getBean() {
        return SpringBeanUtil.getBean(this.getClass());
    }


}
